package com.jinlejia.bbs.logic;

import android.content.Context;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jinlejia.bbs.http.utils.BBSConfig;
import com.jinlejia.bbs.http.utils.HttpCallBack;

import java.util.ArrayList;
import java.util.List;

import static com.jinlejia.bbs.http.utils.HttpUtils.JsonParse;
import static com.jinlejia.bbs.http.utils.HttpUtils.httpCallBack;
import static com.jinlejia.bbs.http.utils.HttpUtils.httpCallForm;
/**
 * @author 王进 on 2019/3/28.
 */

public class SearchLogic {
    private Context context;
    private LogicCallBack callBack;
    public SearchLogic(Context context,LogicCallBack callBack){
        this.context = context;
        this.callBack = callBack;
    }

    public void execute(){
        JSONObject json = new JSONObject();
        httpCallForm(BBSConfig.listSearchRecord,json)
                .execute(httpCallBack(context, new HttpCallBack() {
                    @Override
                    public void onError(Exception e) {

                    }

                    @Override
                    public void onResponse(JsonParse parse) {
                        JSONObject data = parse.data();
                        String array = data.getString("data");
                        if (array==null){
                            callBack.onResult(new ArrayList<String>());
                        }else {
                            List<String> list = JSON.parseArray(array, String.class);
                            callBack.onResult(list);
                        }

                    }

                    @Override
                    public void onNoResponse(int code) {

                    }
                }));
    }

    public interface LogicCallBack{
        void onResult(List<String> result);
    }
}
